package com.weipeep.sys.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.weipeep.common.utils.IdGenerator;
import com.weipeep.sys.dao.UserDao;
import com.weipeep.sys.domain.UserDO;
import com.weipeep.sys.query.StudentQuery;
import com.weipeep.sys.vo.StudentVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.weipeep.sys.dao.StudentDao;
import com.weipeep.sys.domain.StudentDO;
import com.weipeep.sys.service.StudentService;
import com.weipeep.common.base.CoreServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-04-15 10:23:55 | wangyun</small>
 */
@Service
public class StudentServiceImpl extends CoreServiceImpl<StudentDao, StudentDO> implements StudentService {
    @Resource
    UserDao userDao;
    @Resource
    StudentDao studentDao;
    @Autowired
    private IdGenerator idGenerator;
    @Override
    @Transactional
    public boolean insertStudent(StudentVo studentVo) {
        Long userId = idGenerator.nextId();
        Long studentId = idGenerator.nextId();
        UserDO userDO = new UserDO();
        userDO.setBirth(studentVo.getBirth());
        userDO.setSex(studentVo.getSex());
        userDO.setName(studentVo.getRealName());
        userDO.setStatus(1);
        userDO.setId(userId);
        userDO.setMobile(studentVo.getMobile());
        userDO.setEmail(studentVo.getEmail());
        userDO.setLiveAddress(studentVo.getAddress());
        studentVo.setUserId(userId);
        studentVo.setId(studentId);
        Integer userNum = userDao.insert(userDO);
        Integer studentNum = studentDao.insert(studentVo);
        return userNum==1 && studentNum==1;
    }

    @Override
    @Transactional
    public boolean updateStudent(StudentVo student) {
        StudentDO studentDO = studentDao.selectById(student.getId());
        studentDO.setFeeTipTime(student.getFeeTipTime());
        studentDO.setIdCard(student.getIdCard());
        studentDO.setStudentNumber(student.getStudentNumber());
        studentDO.setClassType(student.getClassType());
        Long userId = studentDO.getUserId();
        UserDO userDO = userDao.selectById(userId);
        userDO.setBirth(student.getBirth());
        userDO.setName(student.getRealName());
        userDO.setSex(student.getSex());
        userDO.setMobile(student.getMobile());
        userDO.setEmail(student.getEmail());
        userDO.setLiveAddress(student.getAddress());
        return userDao.updateById(userDO)==1 && studentDao.updateById(studentDO)==1;
    }

    @Override
    public boolean deleteStudentById(Long id) {
        StudentDO studentDO = studentDao.selectById(id);
        return userDao.deleteById(studentDO.getUserId())==1&&studentDao.deleteById(id)==1;
    }

    @Override
    public void deleteStudentBatchIds(List<Long> studentIds) {
        if (studentIds == null || studentIds.size()==0){
            return;
        }
        List<Long> userIds = Lists.newArrayList();
        List<StudentDO> studentDOList = studentDao.selectBatchIds(studentIds);
        studentDOList.forEach(studentDO -> {
            if (studentDO.getUserId()!=null){
                userIds.add(studentDO.getUserId());
            }
        });
        userDao.deleteBatchIds(userIds);
        studentDao.deleteBatchIds(studentIds);
    }

    @Override
    public Page<StudentVo> selectStudentPage(Page<StudentVo> page, StudentVo studentVo) {
        StudentQuery studentQuery = new StudentQuery();
        studentQuery.setOffset(page.getOffset());
        studentQuery.setLimit(page.getLimit());
        if (StringUtils.isNotEmpty(studentVo.getRealName())){
            studentQuery.setRealName(studentVo.getRealName()+"%");
        }
        List<StudentVo> studentVoList = studentDao.selectStudentPage(studentQuery);
        page.setRecords(studentVoList);
        return page;
    }

    @Override
    public int selectStudentCount(StudentVo studentVo) {
        StudentQuery studentQuery = new StudentQuery();
        if (StringUtils.isNotEmpty(studentVo.getRealName())){
            studentQuery.setRealName(studentVo.getRealName()+"%");
        }
        int count = studentDao.selectStudentCount(studentQuery);
        return count;
    }

    @Override
    public List<StudentVo> selectStudentList(StudentVo studentVo) {
        StudentQuery studentQuery = new StudentQuery();
        if (StringUtils.isNotEmpty(studentVo.getRealName())){
            studentQuery.setRealName(studentVo.getRealName()+"%");
        }
        studentQuery.setDeptId(studentVo.getDeptId());
        List<StudentVo> studentVoList = studentDao.selectStudentList(studentQuery);
        return studentVoList;
    }
}
